package J250404;

public class Test1 {
    static int atomicInteger = 0;
    public static void main(String[] args) throws InterruptedException {
//        AtomicInteger atomicInteger = new AtomicInteger(0);

        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 50000; i++) {
//                    atomicInteger.getAndIncrement();
                    synchronized (Test1.class) {
                        atomicInteger++;
                    }
                }
            }
        };
        Thread t1 = new Thread(runnable,"线程1");
        Runnable runnable2 = new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 50000; i++) {
                    synchronized (Test1.class) {
                        atomicInteger++;
                    }
//                    atomicInteger.getAndIncrement();
                }

            }
        };
        Thread t2 = new Thread(runnable2,"线程2");

        t1.start();
        t2.start();

        t1.join();
        t2.join();
        System.out.println("atomicInteger = " + atomicInteger);

    }
}
